package com.chenyang.mediaplay.fragment;

import java.util.ArrayList;

import com.chenyang.mediaplay.R;
import com.chenyang.mediaplay.activity.VideoPlayerActivity;
import com.chenyang.mediaplay.activity.VitamioVideoPlayerActivity;
import com.chenyang.mediaplay.adapter.VideoListAdapter;
import com.chenyang.mediaplay.bean.VideoItem;
import com.chenyang.mediaplay.interfaces.Keys;

import android.content.AsyncQueryHandler;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Video.Media;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class VideoFragment extends BaseFragment {

	private ListView listView;

	@Override
	public void initListener() {
		// 设置条目点击事件
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Cursor cursor = (Cursor) parent.getItemAtPosition(position);
				ArrayList<VideoItem> videos = getVideoItems(cursor);
				enterVideoPlayerActivity(videos, position);
			}

		});

	}
	
	/***
	 * 把Cursor里面的所有数据封装到一个ArrayList中
	 * @param cursor
	 * @return
	 */
	protected ArrayList<VideoItem> getVideoItems(Cursor cursor) {
		ArrayList<VideoItem> videos = new ArrayList<VideoItem>();
		cursor.moveToFirst();
		do {
			videos.add(VideoItem.fromCursor(cursor));
		} while (cursor.moveToNext());
		
		return videos;
	}
	
	/**
	 * 进入视频播放界面
	 * @param videos
	 * @param position
	 */
	protected void enterVideoPlayerActivity(ArrayList<VideoItem> videos, int position) {
		
//		Intent intent = new Intent(getActivity(), VideoPlayerActivity.class);
		Intent intent = new Intent(getActivity(), VitamioVideoPlayerActivity.class);
		intent.putExtra(Keys.ITEMS, videos);
		intent.putExtra(Keys.CURRENT_POSITION, position);
		startActivity(intent);
		
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		// 这个查询方法会运行在主线程
		// getActivity().getContentResolver().query(uri, projection, selection,
		// selectionArgs, sortOrder)
		AsyncQueryHandler queryHandler = new AsyncQueryHandler(getActivity()
				.getContentResolver()) {
			// 查询到数据的回调方法，这个方法会运行在主线程
			@Override
			protected void onQueryComplete(int token, Object cookie,
					Cursor cursor) {
				// TODO Auto-generated method stub
				VideoListAdapter listAdapter = new VideoListAdapter(getActivity(),cursor);
				
				listView.setAdapter(listAdapter);
			}
		};
		int token = 0; // 相当于Message.what
		Object cookie = null; // 相当于Message.obj
		Uri uri = Media.EXTERNAL_CONTENT_URI;
		// 指定要查询哪些列
		String[] projection = { Media._ID, Media.TITLE, Media.DURATION,
				Media.SIZE, Media.DATA, };

		String selection = null;// 指定查询条件
		String[] selectionArgs = null; // 指定查询条件中的参数
		String orderBy = Media.TITLE + " ASC"; // 指定为升序
		// 这个查询方法会运行在子线程
		queryHandler.startQuery(token, cookie, uri, projection, selection,
				selectionArgs, orderBy);
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		listView = (ListView) rootView;
	}

	@Override
	public int getLayoutResID() {
		// TODO Auto-generated method stub
		return R.layout.fragment_video_list;
	}

	@Override
	public void onClick(View v, int id) {
		// TODO Auto-generated method stub

	}

}
